package com.ruoyi.common.core.utils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 时间工具类
 *
 * @author ruoyi
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    private static final DateTimeFormatter yyyyMMddHHmmss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter YYYYMMddHHmmss = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    private static final DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    private static final DateTimeFormatter yyyy = DateTimeFormatter.ofPattern("yyyy");

    private static final long ONE_MILLISECOND = 1000L;
    private static final long ONE_MINUTE = 60000L;
    private static final long ONE_HOUR = 3600000L;
    private static final long ONE_DAY = 86400000L;

    private static final String ONE_SECOND_AGO = "秒前";
    private static final String ONE_MINUTE_AGO = "分钟前";
    private static final String ONE_HOUR_AGO = "小时前";
    private static final String ONE_DAY_AGO = "天前";
    private static final String ONE_MONTH_AGO = "1个月前";
    private static final String TWO_MONTH_AGO = "2个月前";
    private static final String THREE_MONTH_AGO = "3个月前";
    private static final String FOUR_MONTH_AGO = "4个月前";
    private static final String FIVE_MONTH_AGO = "5个月前";
    private static final String SIX_MONTH_AGO = "6个月前";
    private static final String SEVEN_MONTH_AGO = "7个月前";
    private static final String EIGHT_MONTH_AGO = "8个月前";
    private static final String NINE_MONTH_AGO = "9个月前";
    private static final String TEN_MONTH_AGO = "10个月前";
    private static final String ELEVEN_MONTH_AGO = "11个月前";
    private static final String ONE_YEAR_AGO = "1年前";

    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};


    /**
     * 指定格式日期转为Date
     */
    public static Date getDateOfFormat(String date, String patter) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(patter);
        return simpleDateFormat.parse(date);
    }


    /**
     * 多久之前
     */
    public static String format(Date date) {
        Calendar calendar = Calendar.getInstance();
        long now = calendar.getTimeInMillis();
        long delta = now - date.getTime();

        // 逻辑判断
        if (delta < ONE_MINUTE) return delta / ONE_MILLISECOND + ONE_SECOND_AGO;
        if (delta < ONE_HOUR) return delta / ONE_MINUTE + ONE_MINUTE_AGO;
        if (delta < ONE_DAY) return delta / ONE_HOUR + ONE_HOUR_AGO;
        if (delta < getTime(calendar, now)) return delta / ONE_DAY + ONE_DAY_AGO;
        if (delta < getTime(calendar, now)) return ONE_MONTH_AGO;
        if (delta < getTime(calendar, now)) return TWO_MONTH_AGO;
        if (delta < getTime(calendar, now)) return THREE_MONTH_AGO;
        if (delta < getTime(calendar, now)) return FOUR_MONTH_AGO;
        if (delta < getTime(calendar, now)) return FIVE_MONTH_AGO;
        if (delta < getTime(calendar, now)) return SIX_MONTH_AGO;
        if (delta < getTime(calendar, now)) return SEVEN_MONTH_AGO;
        if (delta < getTime(calendar, now)) return EIGHT_MONTH_AGO;
        if (delta < getTime(calendar, now)) return NINE_MONTH_AGO;
        if (delta < getTime(calendar, now)) return TEN_MONTH_AGO;
        if (delta < getTime(calendar, now)) return ELEVEN_MONTH_AGO;
        return ONE_YEAR_AGO;
    }

    /**
     * 获取当前Date
     */
    public static Date currentDate() {
        return new Date();
    }


    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    private static long getTime(Calendar calendar, long now) {
        calendar.add(Calendar.MONTH, -1);
        return now - calendar.getTimeInMillis();
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算时间差
     *
     * @param endDate   最后时间
     * @param startTime 开始时间
     * @return 时间差（天/小时/分钟）
     */
    public static String timeDistance(Date endDate, Date startTime) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startTime.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 相差多少天
     *
     * @param endDate   最后时间
     * @param startTime 开始时间
     * @return 时间差（天/小时/分钟）
     */
    public static Integer timeByDay(Date endDate, Date startTime) {
        if (startTime == null) return 0;
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startTime.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return Math.toIntExact(day);
    }


    /**
     * 相差多少秒
     *
     * @param endDate   最后时间
     * @param startTime 开始时间
     * @return 时间差（天/小时/分钟）
     */
    public static Integer timeBySec(Date endDate, Date startTime) {
        if (endDate == null) return 0;
        if (startTime.getTime() > endDate.getTime()) return 0;
        long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startTime.getTime();
        // 计算差多少秒//输出结果
        long sec = diff / ns;
        return Math.toIntExact(sec);
    }

    /**
     * 计算时间差 00:00:00
     */
    public static String timeDistanceSen(Date endDate, Date startTime) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startTime.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
//        return day + ":" + hour + ":" + min + ":" + sec;

        return hour + ":" + min + ":" + sec;
    }

    public static String convertSecondsToTime(int seconds) {
        if (seconds < 60) {
            return seconds + "秒";
        }
        if (seconds < 3600) {
            int minutes = (seconds % 3600) / 60;
            int remainingSeconds = seconds % 60;
            return minutes + "分" + remainingSeconds + "秒";
        }
        int hours = seconds / 3600;
        int minutes = (seconds % 3600) / 60;
        int remainingSeconds = seconds % 60;
        return hours + "时" + minutes + "分" + remainingSeconds + "秒";
    }


    /**
     * 增加 LocalDateTime ==> Date
     */
    public static Date toDate(LocalDateTime temporalAccessor) {
        ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    /**
     * 增加 LocalDate ==> Date
     */
    public static Date toDate(LocalDate temporalAccessor) {
        LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
        ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }


}
